// MIT license 
//create an jQuery plug in
//many changes by hulahoo
(function($) {
    $.fn.pager = function(options) {
        var defaults = {
            display: 5,
            pageCount: 0,
            currentPage: 0,
            outputNextPrevLinks: true,
            outputFirstAndLastLinks: true,
            outputStartEndLinks: true,
            outputNumberLinks: true,
//          ellipsisText: "...",
            prevText: "上页",
            nextText: "下页",
            startText: "首页",
            endText: "末页"
        };

        var options = $.extend(defaults, options);

        var links;
        var pager;

        if (options.pageCount <= 0) {
            $(this).empty();
            return;
        }

        if (options.currentPage >= options.pageCount) {
            options.currentPage = 0;
        }
        pager = '';

        if (options.pageCount <= 1)
            options.display = -1;

        links = new Array();
        links.length = 0;
        links[0] = 0;

        if (options.display == -1) {
            //links = new int[options.pageCount];
            for (i = 0; i < links.length; i++)
                links[i] = i + 1;
        }
        else {
            //links = new int[options.display];
            links = new Array();
            links.length = options.display;

            // current page in the middle of our range
            var middle = Math.floor(options.display / 2.0) - 1;
            links[middle] = parseInt(options.currentPage);

            // pages preceding current page
            for (i = middle - 1; i >= 0; i--)
                links[i] = links[i + 1] - 1;

            // pages following current page
            for (i = middle + 1; i < links.length; i++)
                links[i] = links[i - 1] + 1;


            // Get rid of page numbers exceeding options.pageCount ("shift" page numbers to the right)
            while (links[links.length - 1] > options.pageCount) {
                for (i = 0; i < links.length; i++)
                    links[i]--;
            }

            // Get rid of 0 or negative pages ("shift" page numbers to the left)
            while (links[0] <= 0) {
                for (i = 0; i < links.length; i++)
                    links[i]++;
            }

            // assign -1 to pages over options.pageCount
            for (i = links.length - 1; i >= 0; i--) {
                if (links[i] > options.pageCount)
                    links[i] = -1;
                else
                    break;
            }
        }

        pager += "<div id='pager' class='pagination pagination-small pagination-centered'><ul>";
        if (options.outputStartEndLinks) {
            if (options.currentPage != 0)
                pager += "<li class='active active_s' page='0'><a href='javascript:;'>" + options.startText + "</a></li>";
            else 
                pager += "<li class='disabled'><span>" + options.startText + "</span></li>";
        }

        if (options.outputNextPrevLinks) {
            if ((options.currentPage + 1) > 1)
                pager += "<li class='active active_s' page='" + (options.currentPage - 1) + "'><a href='javascript:;'>" + options.prevText + "</a></li>";
            else 
                pager += "<li class='disabled'><span>" + options.prevText + "</span></li>";
        }

//        if (links[0] != 1) {
//            if (options.outputFirstAndLastLinks) {
//                if ((options.currentPage + 1) == 1) {
//                    pager += "<li ";
//                    pager += "class='disabled'><span>" + 1 + "</span></li>";
//                }
//                else {
//                    pager += "<li ";
//                    pager += "class='active active_s' page='0'><a href='javascript:;'>" + 1 + "</a></li>";
//                }
//            }
//
//            if(options.outputNumberLinks) {
//               if (links[0] != 2) {
//                   pager += "<li class='disabled'><span>" + options.ellipsisText + "</span></li>";
//               }
//            }
//        }

        if(options.outputNumberLinks) {
          for (i = 0; i < links.length; i++) {
            if (links[i] == -1)
                break;

            pager += "<li ";

            if ((options.currentPage + 1) == links[i])
                pager += "class='disabled'"
            else
                pager += "class='active active_s'"

            pager += " page='" + (links[i] - 1) + "'><a href='javascript:;'>" + links[i] + "</a></li>"

            if (options.display == -1)
                pager += " "; // write a space so page numbers can wrap if displaying ALL page links
          }

//          if (links[links.length - 1] != -1 && links[links.length - 1] != options.pageCount) {
//            if (links[links.Length - 1] != options.pageCount - 1) {
//                pager += "<li class='disabled'><span>" + options.ellipsisText + "</span></li>";
//            }
//
//            // output link to last page number
//            if (options.outputFirstAndLastLinks) {
//               pager += "<li class='active active_s'";
//               pager += " page='" + (options.pageCount - 1) + "'><a href='javascript:;'>" + options.pageCount + "</a></li>"
//            }
//
//          }
        }

        if (options.outputNextPrevLinks) {
            if ((options.currentPage + 1) < options.pageCount)
                pager += "<li class='active active_s' page='" + (options.currentPage + 1) + "'><a href='javascript:;'>" + options.nextText + "</a></li>"
            else
                pager += "<li class='disabled' ><span>" + options.nextText + "</span></li>";
        }

        if (options.outputStartEndLinks) {
            if ((options.currentPage + 1) < options.pageCount)
                pager += "<li class='active active_s' page='" +(options.pageCount- 1) + "'><a href='javascript:;'>" + options.endText + "</a></li>";
            else 
                pager += "<li class='disabled'><span>" + options.endText + "</span></li>";
        }

        pager + "</ul></div>";

        $(this).empty();
        $(pager).appendTo(this);

        return this;    
    };
}
)(jQuery);
